home *** CD-ROM | disk | FTP | other *** search
-
- /* =============================
- Rubber Bandit is a simple
- srcXor rubber-banding example.
-
- Think C 4.0.4 source.
-
- - DMH, MacDTS, 3/5/91
- ============================= */
-
- /* constants: */
-
- #define testDLOG 1234
- #define OK_button 1
-
- /* function prototypes: */
-
- extern void HandleDLOG(DialogPtr theDialog);
- extern pascal Boolean RubberProc(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
- extern void RubberBandIt(Point anchorPt);
- extern Rect *CheckRect(Rect *theRect);
- extern void DrawStuff(Rect *rubberRect);
- extern void FanStuff(Rect *theRect);
-
-
- main()
- {
- DialogPtr theDialog = NULL;
-
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- theDialog = GetNewDialog(testDLOG, NULL, (WindowPtr) -1L);
- if (theDialog != NULL) HandleDLOG(theDialog);
-
- ExitToShell();
- }
-
-
- /* ================================================
- HandleDLOG calls ModalDialog and rubber-bands
- until the OK button is pressed.
- ================================================ */
-
- void HandleDLOG(theDialog)
- DialogPtr theDialog;
- {
- short itemHit;
- GrafPtr oldPort;
- Point anchorPt;
-
- do
- {
- ModalDialog(NULL, &itemHit);
-
- if (itemHit != OK_button)
- {
- GetPort(&oldPort); /* Save the port, set it to our dialog, */
- SetPort(theDialog);
- GetMouse(&anchorPt); /* get the current mouse pos. and */
- RubberBandIt(anchorPt); /* rubber-band the image. */
- SetPort(oldPort); /* Restore the original port. */
- }
- }
- while (itemHit != OK_button);
-
- DisposDialog(theDialog); /* Throw away the dialog. */
- }
-
-
- /* ================================================
- RubberBandIt follows the mouse and rubber-bands
- a design based on its position. It retains
- control until the mouse button is released.
- ================================================ */
-
- void RubberBandIt(anchorPt)
- Point anchorPt;
- {
- Point curMousePt = {0, 0};
- Rect rubberRect;
-
- PenMode(srcXor); /* Set pen mode to exclusive or.*/
- PenPat(gray);
-
- GetMouse(&curMousePt); /* Get current mouse pos. */
-
- rubberRect.top = anchorPt.v; /* Draw initial rectangle. */
- rubberRect.left = anchorPt.h;
- rubberRect.bottom = curMousePt.v;
- rubberRect.right = curMousePt.h;
- DrawStuff(&rubberRect);
-
- while (Button())
- {
- GetMouse(&curMousePt); /* Get current mouse pos… */
-
- if (curMousePt.h != rubberRect.right || /* If mouse moved… */
- curMousePt.v != rubberRect.bottom)
- {
- DrawStuff(&rubberRect); /* Erase old… */
- rubberRect.bottom = curMousePt.v;
- rubberRect.right = curMousePt.h;
- DrawStuff(&rubberRect); /* and draw new. */
- }
- }
-
- DrawStuff(&rubberRect); /* Erase old at end. */
- }
-
-
- /* ================================================
- DrawStuff draws designs in the rectangle
- passed, using the current pen mode, etc.
- ================================================ */
-
- void DrawStuff(rubberRect)
- Rect *rubberRect;
- {
- Rect curRect;
-
- curRect = *rubberRect;
- FrameRect(CheckRect(&curRect));
- FanStuff(CheckRect(&curRect));
- }
-
-
- /* ================================================
- FanStuff draws a fanlike thing in the rect
- passed, using the current pen mode, etc.
- ================================================ */
-
- void FanStuff(theRect)
- Rect *theRect;
- {
- short ang;
-
- for (ang = 0; ang < 360; ang += 60)
- PaintArc(theRect, ang, 45);
- }
-
-
- /* ================================================
- CheckRect makes sure that the top of the
- rectangle passed is ≤ the bottom and the left is
- ≤ the right. FrameRect needs things this way.
- ================================================ */
-
- Rect *CheckRect(theRect)
- Rect *theRect;
- {
- short temp;
-
- if (theRect->top > theRect->bottom) /* Need to reverse top and bottom? */
- {
- temp = theRect->top;
- theRect->top = theRect->bottom;
- theRect->bottom = temp;
- }
-
- if (theRect->left > theRect->right) /* Need to reverse left and right? */
- {
- temp = theRect->left;
- theRect->left = theRect->right;
- theRect->right = temp;
- }
-
- return theRect; /* This makes nested calls easier. */
- }
-
-
-